package com.sinog.biz.service.business.impl;

import com.sinog.biz.BizApplication;
import com.sinog.biz.mapper.havexml.other.DsSqlMapper;
import com.sinog.biz.service.business.PageSelectService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.JdbcUtils;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import static com.sinog.biz.BizApplication.applicationContext;

/**
 * @desc 页面查询
 * @author ybl
 * @date 2022/10/19
 */
@Slf4j
@Service
public class PageSelectServiceImpl implements PageSelectService {

    @Resource
    private DsSqlMapper dsSqlMapper;
    private JdbcTemplate bujiJdbcTemplate;

    @Override
    public Result getPageSelect(String data) {
        Map<String,Object> map = JsonUtil.toMap(data);
        List<Map<String,Object>> mapList;
        String sql = (String)map.get("selectSql");
        sql = sql.replace("&lt;", "<");
        sql = sql.replace("&gt;", ">");
        try {
            // 0 本省， 1 司法部
            if(Constants.NumberStr.NUM_STR_ZERO.equals(map.get("sqlType"))){
                mapList = dsSqlMapper.selectSqlList(sql);
            }else {
                ApplicationContext applicationContext = BizApplication.applicationContext;
                bujiJdbcTemplate = (JdbcTemplate)applicationContext.getBean("bujiJdbcTemplate");
                mapList = bujiJdbcTemplate.queryForList(sql);
            }
            return Result.success(mapList);
        }catch(Exception e){
            log.error(e.getMessage());
            log.error("=========》页面查询SQL错误执行：{}",map.get("selectSql"));
        }
        return Result.failure("SQL有问题");
    }

    /**
     * 运维功能-删除罪犯人员信息
     * @param codeId 目的省区域编码
     * @param priNum 罪犯人员编号
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteRyByPriNum(String codeId,String priNum){
        bujiJdbcTemplate = (JdbcTemplate)applicationContext.getBean("middleJdbcTemplate");
        String localCode = CodeUrlUtils.getLocalCode();
        executeDelSql(priNum,localCode,codeId);
        return Result.success(localCode+"删除罪犯编号:"+priNum+"信息同步中间库成功");
    }

    /**
     * 描述...
     * @param priNum priNum
     * @param localCode localCode
     * @param jzCode jzCode
     */
    public void executeDelSql(String priNum,String localCode,String jzCode){
        String anZhiDeleteSql = "delete from ab_anzhi where pri_num='" + priNum + "'";
        String bangFuDeleteSql = "delete from ab_bangfu where pri_num='" + priNum + "'";
        String jianDingDeleteSql = "delete from ab_cun_jianding where pri_num='" + priNum + "'";
        String genZongBjDeleteSql = "delete from ab_genzongbangjiao where pri_num='" + priNum + "'";
        String jiuZhuDeleteSql = "delete from ab_jiu where pri_num='" + priNum + "'";
        String lieGuanDeleteSql = "delete from ab_lieguan where pri_num='" + priNum + "'";
        String avDeleteSql = "delete from ab_mv where pri_num='" + priNum + "'";
        String paiChaDeleteSql = "delete from ab_paicha where pri_num='" + priNum + "'";
        String abPrisonersDeleteSql = "delete from ab_prisoners where pri_num='" + priNum + "'";
        String abPrisonersJcDeleteSql = "delete from ab_prisoners_jcab where pri_num='" + priNum + "'";
        String shJzDeleteSql = "delete from ab_shehuijiuzhu where pri_num='" + priNum + "'";
        String tuoGuanDeleteSql = "delete from ab_tuoguan where pri_num='" + priNum + "'";
        String xiaoZuDeleteSql = "delete from ab_xiaozu where pri_num='" + priNum + "'";
        String zouFangDeleteSql = "delete from ab_zoufang where pri_num='" + priNum + "'";
        String hnInterfaceOppositeDeleteSql = "delete from hn_interface_opposite where pri_num='" + priNum + "'";
        String hnInterfaceOppositeLogDeleteSql = "delete from hn_interface_opposite_log where pri_num='" + priNum + "'";
        String mainExceltempDeleteSql = "delete from main_exceltemp where pri_num='" + priNum + "'";
        String prisaveTempDeleteSql = "delete from prisave_temp where pri_num='" + priNum + "'";
        String prisonerCjpgDeleteSql = "delete from prisoner_cjpg where pri_num='" + priNum + "'";
        String prisonerFxDeleteSql = "delete from prisoner_fx where pri_num='" + priNum + "'";
        String prisonerHcDeleteSql = "delete from prisoner_hc where pri_num='" + priNum + "'";
        String prisonerMainDeleteSql = "delete from prisoner_main where pri_num='" + priNum + "'";
        String prisonerSrDeleteSql = "delete from prisoner_sr where pri_num='" + priNum + "'";
        String prisonerTransferDeleteSql = "delete from prisoner_transfer where pri_num='" + priNum + "'";
        String prisonerTsDeleteSql = "delete from prisoner_ts where pri_num='" + priNum + "'";
        String prisonerZnDeleteSql = "delete from prisoner_zn where pri_num='" + priNum + "'";
        String prisonerZzDeleteSql = "delete from prisoner_zz where pri_num='" + priNum + "'";
        String prisonersJxjsDeleteSql = "delete from prisoners_jxjs where pri_num='" + priNum + "'";
        String prisonersZyjnDeleteSql = "delete from prisoners_zyjn where pri_num='" + priNum + "'";
        String srExceltempDeleteSql = "delete from sr_exceltemp where pri_num='" + priNum + "'";
        String uploadAppendixFileDeleteSql = "delete from upload_appendix_file where pri_num='" + priNum + "'";
        String xtjcKsywxtDeleteSql = "delete from xtjc_ksywxt where pri_num='" + priNum + "'";
        String znExceltempDeleteSql = "delete from zn_exceltemp where pri_num='" + priNum + "'";
        String prisonerXjxxDeleteSql = "delete from prisoner_xjxx where pri_num='" + priNum + "'";
        String middleSql1 = JdbcUtils.createMiddleSql(priNum,anZhiDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql1);
        String middleSql2 = JdbcUtils.createMiddleSql(priNum,bangFuDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql2);
        String middleSql3 = JdbcUtils.createMiddleSql(priNum,jianDingDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql3);
        String middleSql4 = JdbcUtils.createMiddleSql(priNum,genZongBjDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql4);
        String middleSql5 = JdbcUtils.createMiddleSql(priNum,jiuZhuDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql5);
        String middleSql6 = JdbcUtils.createMiddleSql(priNum,lieGuanDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql6);
        String middleSql7 = JdbcUtils.createMiddleSql(priNum,avDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql7);
        String middleSql8 = JdbcUtils.createMiddleSql(priNum,paiChaDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql8);
        String middleSql10 = JdbcUtils.createMiddleSql(priNum,abPrisonersDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql10);
        String middleSql11 = JdbcUtils.createMiddleSql(priNum,abPrisonersJcDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql11);
        String middleSql12 = JdbcUtils.createMiddleSql(priNum,shJzDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql12);
        String middleSql13 = JdbcUtils.createMiddleSql(priNum,tuoGuanDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql13);
        String middleSql14 = JdbcUtils.createMiddleSql(priNum,xiaoZuDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql14);
        String middleSql15 = JdbcUtils.createMiddleSql(priNum,zouFangDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql15);
        String middleSql16 = JdbcUtils.createMiddleSql(priNum,hnInterfaceOppositeDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql16);
        String middleSql17 = JdbcUtils.createMiddleSql(priNum,hnInterfaceOppositeLogDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql17);
        String middleSql18 = JdbcUtils.createMiddleSql(priNum,mainExceltempDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql18);
        String middleSql20 = JdbcUtils.createMiddleSql(priNum,prisaveTempDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql20);
        String middleSql21 = JdbcUtils.createMiddleSql(priNum,prisonerCjpgDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql21);
        String middleSql22 = JdbcUtils.createMiddleSql(priNum,prisonerFxDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql22);
        String middleSql23 = JdbcUtils.createMiddleSql(priNum,prisonerHcDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql23);
        String middleSql24 = JdbcUtils.createMiddleSql(priNum,prisonerMainDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql24);
        String middleSql25 = JdbcUtils.createMiddleSql(priNum,prisonerSrDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql25);
        String middleSql26 = JdbcUtils.createMiddleSql(priNum,prisonerTransferDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql26);
        String middleSql27 = JdbcUtils.createMiddleSql(priNum,prisonerTsDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql27);
        String middleSql28 = JdbcUtils.createMiddleSql(priNum,prisonerZnDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql28);
        String middleSql29 = JdbcUtils.createMiddleSql(priNum,prisonerZzDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql29);
        String middleSql30 = JdbcUtils.createMiddleSql(priNum,prisonersJxjsDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql30);
        String middleSql31 = JdbcUtils.createMiddleSql(priNum,prisonersZyjnDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql31);
        String middleSql32 = JdbcUtils.createMiddleSql(priNum,srExceltempDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql32);
        String middleSql33 = JdbcUtils.createMiddleSql(priNum,uploadAppendixFileDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql33);
        String middleSql34 = JdbcUtils.createMiddleSql(priNum,xtjcKsywxtDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql34);
        String middleSql35 = JdbcUtils.createMiddleSql(priNum,znExceltempDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql35);
        String middleSql36 = JdbcUtils.createMiddleSql(priNum,prisonerXjxxDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
        bujiJdbcTemplate.update(middleSql36);
    }
}